package com.chx.mydubbo.consumer;

import com.chx.mydubbo.client.MyDubboClient;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author 夏天
 * @date 2018/5/5 下午3:41
 * @Description:
 */
public class Main {
    private static String ip;
    private static int port;

    private static HelloService helloServiceProxy;

    private static HelloServiceImpl helloServiceImplProxy;


    public static void main(String[] args) throws Exception {
        // 1.系统启动，扫描 @ChxFeginClient 注解的类
        Class clzss = Class.forName("com.chx.mydubbo.consumer.HelloService");

        if (clzss.isAnnotationPresent(ChxFeginClient.class)) {
            ChxFeginClient ChxFeginClient = (ChxFeginClient) clzss.getAnnotation(ChxFeginClient.class);
            ip = ChxFeginClient.ip();
            port = ChxFeginClient.port();
        }

        // 2.生成HelloService 接口的一个代理
        helloServiceProxy = (HelloService) Proxy.newProxyInstance(HelloService.class.getClassLoader(), new Class[]{HelloService.class}, new InvocationHandler() {

            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                if (method.isAnnotationPresent(ChxDubboFegin.class)) {
                    ChxDubboFegin chxDubboFegin = method.getAnnotation(ChxDubboFegin.class);

                    String interfaceName = chxDubboFegin.interfaceName();
                    String version = chxDubboFegin.version();
                    String methodName = chxDubboFegin.methodName();
                    String paramsKeys = chxDubboFegin.paramsKeys();
                    String paramsValues = chxDubboFegin.paramsValues();

                    return new MyDubboClient(ip, port).dubboConsumer(interfaceName, version, methodName, paramsKeys, paramsValues);
                }


                return null;
            }
        });

        // 3.调用代理对象的方法
        System.out.println(helloServiceProxy.sayHello("chx"));


        helloServiceProxy.a();
    }
}
